package com.xyzwps.benchmarks.util;

import java.util.List;

public class TablePrinter {

    public static void print(List<String> header, List<List<String>> data) {
        int cols = header.size();
        int[] colWidth = new int[cols];
        for (int i = 0; i < cols; i++) {
            colWidth[i] = header.get(i).length();
        }

        for (var row : data) {
            for (int i = 0; i < cols; i++) {
                var it = row.get(i);
                colWidth[i] = Math.max(it.length(), colWidth[i]);
            }
        }

        StringBuilder sb = new StringBuilder();
        // top border
        for (int i = 0; i < cols; i++) {
            sb.append(i == 0 ? '╭' : '┬');
            int width = colWidth[i] + 2;
            sb.append("─".repeat(width));
        }
        sb.append("╮\n");
        // header
        for (int i = 0; i < cols; i++) {
            sb.append('│');
            int width = colWidth[i];
            String value = header.get(i);
            sb.append(' ').append(value).append(" ".repeat(width - value.length() + 1));
        }
        sb.append("│\n");

        for (var row : data) {
            // top border
            for (int i = 0; i < cols; i++) {
                sb.append(i == 0 ? '├' : '┼');
                int width = colWidth[i] + 2;
                sb.append("─".repeat(width));
            }
            sb.append("┤\n");
            // data
            for (int i = 0; i < cols; i++) {
                sb.append('│');
                int width = colWidth[i];
                String value = row.get(i);
                sb.append(' ').append(value).append(" ".repeat(width - value.length() + 1));
            }
            sb.append("│\n");
        }

        // bottom border
        for (int i = 0; i < cols; i++) {
            sb.append(i == 0 ? '╰' : '┴');
            int width = colWidth[i] + 2;
            sb.append("─".repeat(width));
        }
        sb.append("╯\n");

        System.out.println(sb.toString());

//        ╭─┬╮
//        │ ││
//        ├─┼┤
//        ╰─┴╯

    }
}
